# Copyright 2024 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_test")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "boolean_constraint_value", "incompatible_with_mcu")
load("//pw_build:merge_flags.bzl", "flags_from_dict")
load(
    "//pw_protobuf_compiler:pw_proto_library.bzl",
    "pw_proto_filegroup",
    "pwpb_proto_library",
    "pwpb_rpc_proto_library",
)
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")
load(":config.bzl", "PW_GRPC_PW_RPC_CONFIG_OVERRIDES")

package(
    default_visibility = ["//visibility:public"],
)

log_defines = [
    "PW_LOG_MODULE_NAME=\\\"GRPC\\\"",
    "PW_LOG_LEVEL=PW_LOG_LEVEL_INFO",
]

# In order to use this module, it must be explicitly enabled by the platform
# since it requires a non-default pw_rpc config_override to build and function.
# The required pw_rpc config options are provided by the
# PW_GRPC_PW_RPC_CONFIG_OVERRIDES defines array in config.bzl.
#
# Since we can't automatically determine whether those overrides are set, we
# require the module to be explicitly enabled using this constraint.
boolean_constraint_value(
    name = "enabled",
)

# Required config options to apply to //pw_rpc:config_override. This can be used
# to override the pw_rpc config on the command line or on a platforms flags:
#   --//pw_rpc:config_override=//pw_grpc:pw_rpc_config
cc_library(
    name = "pw_rpc_config",
    defines = PW_GRPC_PW_RPC_CONFIG_OVERRIDES,
)

cc_library(
    name = "connection",
    srcs = [
        "connection.cc",
    ],
    hdrs = [
        "public/pw_grpc/connection.h",
    ],
    implementation_deps = ["//pw_assert:check"],
    local_defines = log_defines,
    strip_include_prefix = "public",
    target_compatible_with = [":enabled"],
    deps = [
        ":hpack",
        ":send_queue",
        "//pw_allocator:allocator",
        "//pw_allocator:synchronized_allocator",
        "//pw_async:dispatcher",
        "//pw_async_basic:dispatcher",
        "//pw_bytes",
        "//pw_chrono:system_clock",
        "//pw_containers:dynamic_queue",
        "//pw_function",
        "//pw_log",
        "//pw_numeric:checked_arithmetic",
        "//pw_result",
        "//pw_span",
        "//pw_status",
        "//pw_stream",
        "//pw_string",
        "//pw_string:string",
        "//pw_sync:inline_borrowable",
        "//pw_thread:thread",
        "//pw_thread:thread_core",
    ],
)

cc_library(
    name = "send_queue",
    srcs = ["send_queue.cc"],
    hdrs = ["public/pw_grpc/send_queue.h"],
    local_defines = log_defines,
    strip_include_prefix = "public",
    deps = [
        "//pw_allocator",
        "//pw_async:dispatcher",
        "//pw_async_basic:dispatcher",
        "//pw_bytes",
        "//pw_containers:dynamic_deque",
        "//pw_function",
        "//pw_log",
        "//pw_multibuf",
        "//pw_multibuf:allocator",
        "//pw_result",
        "//pw_span",
        "//pw_status",
        "//pw_stream",
        "//pw_string",
        "//pw_sync:lock_annotations",
        "//pw_sync:mutex",
        "//pw_thread:thread",
        "//pw_thread:thread_core",
    ],
)

cc_library(
    name = "grpc_channel_output",
    hdrs = ["public/pw_grpc/grpc_channel_output.h"],
    strip_include_prefix = "public",
    deps = [
        ":connection",
        "//pw_bytes",
        "//pw_rpc",
    ],
)

cc_library(
    name = "pw_rpc_handler",
    srcs = ["pw_rpc_handler.cc"],
    hdrs = ["public/pw_grpc/pw_rpc_handler.h"],
    local_defines = log_defines,
    strip_include_prefix = "public",
    target_compatible_with = [":enabled"],
    deps = [
        ":connection",
        ":grpc_channel_output",
        "//pw_bytes",
        "//pw_log",
        "//pw_result",
        "//pw_rpc",
        "//pw_rpc_transport:rpc_transport",
        "//pw_status",
        "//pw_string",
        "//pw_sync:inline_borrowable",
    ],
)

cc_library(
    name = "hpack",
    srcs = [
        "hpack.autogen.inc",
        "hpack.cc",
    ],
    hdrs = [
        "pw_grpc_private/hpack.h",
    ],
    implementation_deps = ["//pw_assert:check"],
    local_defines = log_defines,
    tags = ["noclangtidy"],
    deps = [
        "//pw_bytes",
        "//pw_log",
        "//pw_result",
        "//pw_span",
        "//pw_status",
        "//pw_string:builder",
        "//pw_string:string",
        "//pw_string:util",
    ],
)

pw_cc_test(
    name = "hpack_test",
    srcs = ["hpack_test.cc"],
    tags = ["noclangtidy"],
    deps = [
        ":hpack",
        "//pw_bytes",
    ],
)

cc_binary(
    name = "test_pw_rpc_server",
    srcs = ["test_pw_rpc_server.cc"],
    deps = [
        ":connection",
        ":echo_pwpb_rpc",
        ":grpc_channel_output",
        ":pw_rpc_handler",
        "//pw_allocator:best_fit",
        "//pw_allocator:libc_allocator",
        "//pw_assert_basic:pw_assert_basic_handler",
        "//pw_assert_log:assert_backend",
        "//pw_assert_log:check_backend",
        "//pw_bytes",
        "//pw_checksum",
        "//pw_log",
        "//pw_result",
        "//pw_rpc",
        "//pw_rpc_transport:service_registry",
        "//pw_span",
        "//pw_status",
        "//pw_stream",
        "//pw_stream:socket_stream",
        "//pw_string",
        "//pw_thread:test_thread_context",
        "//pw_thread:thread",
    ],
)

pw_proto_filegroup(
    name = "echo_proto_and_options",
    srcs = ["examples/echo/echo.proto"],
    options_files = ["examples/echo/echo.options"],
)

proto_library(
    name = "echo_proto",
    srcs = [":echo_proto_and_options"],
)

pwpb_proto_library(
    name = "echo_pwpb",
    deps = [":echo_proto"],
)

pwpb_rpc_proto_library(
    name = "echo_pwpb_rpc",
    pwpb_proto_library_deps = [":echo_pwpb"],
    deps = [":echo_proto"],
)

platform(
    name = "test_platform",
    constraint_values = [":enabled"],
    flags = flags_from_dict({
        "@pigweed//pw_rpc:config_override": "@pigweed//pw_grpc:pw_rpc_config",
    }),
    parents = ["@bazel_tools//tools:host_platform"],
    visibility = ["//visibility:private"],
)

# This test requires a special configuration. It's run in CI, and can be
# run manually via:
#
#   bazel test --platforms=//pw_grpc:test_platform //pw_grpc:integration_test
go_test(
    name = "integration_test",
    srcs = ["integration_test.go"],
    data = [
        ":test_pw_rpc_server",
    ],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        "@org_golang_google_grpc//:grpc",
        "@org_golang_google_grpc//codes",
        "@org_golang_google_grpc//credentials/insecure",
        "@org_golang_google_grpc//status",
        "@org_golang_google_grpc_examples//features/proto/echo",
        "@org_golang_google_grpc_examples//helloworld/helloworld",
    ],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "docs.rst",
    ],
    prefix = "pw_grpc/",
    target_compatible_with = incompatible_with_mcu(),
)
